<template>
	<view style="min-height: 100vh;background: #f6f8fa;">
		<view class="t-login">
			<u-navbar title=" " @rightClick="rightClick" :autoBack="true" :bgColor="bgColor">
			</u-navbar>
			<view class="bj-left"></view>
			<view class="bj-right"></view>
			<view class="t-b" :style="{paddingTop:paddingTop}">欢迎使用情绪算法</view>
			<image class="img-icon" src="https://baxztech.com/bian_admin_api/public/bianmp/bxicon2.png"></image>
			<view class="h30"></view>
			<view class="h30"></view>
			<form class="cl">
				<view class="t-a">
					<image src="@/static/images/sj.png"></image>
					<input @focus="getHeight" @blur="leaveInput" :adjust-position="true" type="number" name="phone"
						placeholder="请输入手机号" maxlength="11" v-model="phone" />
				</view>
				<view class="t-a">
					<image src="@/static/images/yz.png"></image>
					<input @focus="getHeight" @blur="leaveInput" :always-embed="true" :cursor-spacing="10" :adjust-position="true" @keyboardheightchange="keyboardheightchange" type="number" name="code"
						maxlength="6" placeholder="请输入验证码" v-model="yzm" />
					<view v-if="showText" class="t-c" @click="getCode()">发送短信</view>
					<view v-else class="t-c" style="background-color: #A7A7A7;">重新发送({{ second }})</view>
				</view>
				<button @click="login()">登 录</button>
			</form>
<!-- 			<view class="footer">
				登录及接受<text class="fwtext">用户协议</text> 及 <text class="fwtext">隐私授权</text>
			</view> -->
		</view>
		<view class="footer">
			<checkbox-group @change="checkChange">
				<label>
					<checkbox color="#1d6efa" value="cb" />
					同意<text @click.stop="openPrivacyContract" class="fwtext">用户隐私</text> 授权
				</label>
			</checkbox-group>
		</view>
	</view>

</template>
<script>
	export default {
		data() {
			return {
				paddingTop: '300rpx',
				checkboxValue: '',
				title: '登录',
				second: 60,
				showText: true,
				phone: '',
				yzm: '',
				bgColor: 'rgba(0,0,0,0)',
				check: false,
			};
		},
		onLoad() {},
		methods: {
			keyboardheightchange(e){
				// console.log(e);
			},
			checkChange(e){
				if(e.detail.value.length){
					this.check = true;
				}else{
					this.check = false;
				}
			},
			openPrivacyContract() {
				wx.openPrivacyContract({
					fail: (error) => {
						console.log(error)
					}
				})
			},
			//当前登录按钮操作
			login() {
				var that = this;
				if (!that.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(that.phone)) {
					uni.showToast({
						title: '请输入正确手机号',
						icon: 'none'
					});
					return;
				}
				if (!that.yzm) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return;
				}
				uni.$u.http.post('/client/login_code', {
					phone: this.phone,
					code: this.yzm
				}).then(res => {
					if (res.status == 200) {
						this.$store.state.tiredInfo.title = ""
						this.$store.state.stressInfo.title = ""
						this.$store.state.emotionInfo.title = ""
						uni.setStorageSync('phone', this.phone);
						uni.reLaunch({
							url: '/pages/index/emotion'
						})
					} else {
						uni.$u.toast(res.msg || '登录失败！')
					}
				})
			},
			//离开输入框触发
			leaveInput(e) {
				// this.paddingTop = '300rpx';
			},
			//点击输入框触发
			getHeight(e) {
				// this.paddingTop = '0rpx';
			},
			radioChange(e) {
				if (e.length) {
					this.check = true;
				} else {
					this.check = false;
				}
			},
			//获取短信验证码
			getCode() {
				if(!this.check){
					return uni.showToast({
						icon:'none',
						title:'请先勾选同意用户隐私协议和授权！'
					})
				}
				var that = this;
				//这里请求后台获取短信验证码
				uni.showLoading({
					title: '正在获取验证码',
					mask:true
				})
				if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(that.phone)) {
					uni.hideLoading();
					uni.showToast({
						title: '请输入正确手机号',
						icon: 'none'
					});
					return;
				}
				uni.$u.http.post('/client/get_code', {
					phone: this.phone
				}).then(res => {
					that.showText = false;
					uni.hideLoading();
					
					if (res.status == 200) {
						uni.$u.toast('验证码已发送！');
						var interval = setInterval(() => {
							var times = that.second - 1;
							that.second = times < 10 ? '0' + times : times; //小于10秒补 0
							// that.second = times;
						}, 1000);
						setTimeout(() => {
							clearInterval(interval);
							that.second = 60;
							that.showText = true;
						}, 60000);
					} else {
						uni.$u.toast('验证码获取失败！');
					}
				}).catch(() => {
					that.showText = true;
					uni.hideLoading();
					uni.$u.toast('网络错误！');
				})
			},
		}
	};
</script>
<style>
	.t-login {
		width: 600rpx;
		margin: 0 auto;
		font-size: 28rpx;
		color: #000;
	}

	.t-login button {
		font-size: 28rpx;
		background:  linear-gradient(#1d6efa, #1d6efa), #1d6efa;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		box-shadow: 0 5px 7px 0 rgba(86, 119, 252, 0.2);
	}
	.img-icon{
		  width: 224rpx;
		  height: 224rpx;
	}
	.t-login input {
		padding: 0 20rpx 0 120rpx;
		height: 90rpx;
		line-height: 90rpx;
		margin-bottom: 50rpx;
		background: #f8f7fc;
		border: 1px solid #e9e9e9;
		font-size: 28rpx;
		border-radius: 50rpx;
	}
	.fwtext{
		color: #000;
		font-weight: 500;
		margin: 0 8rpx;
	}
	.h30{
		height: 30rpx;
	}
	.bj-left {
		position: absolute;
		top: 0rpx;
		left: 2rpx;
		width: 406rpx;
		height: 492rpx;
		background: #9cd2ff4d;
		filter: blur(160rpx);
	}

	.bj-right {
		position: absolute;
		top: 0rpx;
		left: 380rpx;
		width: 372rpx;
		height: 412rpx;
		background: linear-gradient(133.85deg, #9cd2ff00 77.38%, #c1a5fa2e 15.32%, #9cd2ff2e 37.58%);
		filter: blur(60rpx);
	}

	.t-login .t-a {
		position: relative;
		z-index: 999;
	}

	.t-login .t-a image {
		width: 40rpx;
		height: 40rpx;
		position: absolute;
		left: 40rpx;
		top: 28rpx;
		border-right: 2rpx solid #dedede;
		padding-right: 20rpx;
	}

	.t-login .t-b {
		text-align: left;
		font-size: 46rpx;
		font-weight: 500;
		color: #333333;
		padding: 0 0 40rpx 0;
		font-weight: bold;
	}

	.t-login .t-c {
		z-index: 9999;
		position: absolute;
		right: 22rpx;
		top: 22rpx;
		background: linear-gradient(#1d6efa, #1d6efa), #1d6efa;
		color: #fff;
		font-size: 24rpx;
		border-radius: 50rpx;
		height: 50rpx;
		line-height: 50rpx;
		padding: 0 25rpx;
	}

	.t-login .t-d {
		text-align: center;
		color: #999;
		margin: 80rpx 0;
	}

	.t-login .t-e {
		text-align: center;
		width: 250rpx;
		margin: 80rpx auto 0;
	}

	.t-login .t-g {
		float: left;
		width: 50%;
	}

	.t-login .t-e image {
		width: 50rpx;
		height: 50rpx;
	}

	.t-login .t-f {
		text-align: center;
		margin: 200rpx 0 0 0;
		color: #666;
	}

	.t-login .t-f text {
		margin-left: 20rpx;
		color: #aaaaaa;
		font-size: 27rpx;
	}

	.t-login .uni-input-placeholder {
		color: #000;
	}

	.cl {
		zoom: 1;
	}

	.cl:after {
		clear: both;
		display: block;
		visibility: hidden;
		height: 0;
		content: '\20';
	}

	.footer {
		margin-top: 20px;
		display: flex;
		font-size: 14px;
		align-items: center;
		justify-content: center;
		color: #919191;
	}
</style>